<?php

namespace app\model;

use app\traits\ModelTrait;
use think\helper\Str;
use think\Model;

class VipOrder extends Model
{
    use ModelTrait;

    public array $notes = [
        'status'   => [
            1 => '支付成功',
            2 => '支付失败',
            3 => '待支付',
            4 => '已退款',
        ],
        'pay_type' => [
            1  => '支付宝支付',
            2  => '微信支付',
            3  => '苹果支付',
            99 => '管理员添加',
        ],
    ];

    public function getPaymentAttr($value)
    {
        return $value ? json_decode($value, true) : [];
    }

    public function userInfo(): \think\model\relation\HasOne
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,mobile,nickname,avatar,unionid,os_type,pitcher_id,launch_task_id,channel_id');
    }

    /**
     * @param int   $uid
     * @param array $vip_detail
     * @return bool
     */
    public static function admin_add_vip($uid = 0, $vip_detail = []): bool
    {
        if (empty($uid)) return false;
        if (empty($vip_detail)) return false;
        list($_u_sec, $_sec) = explode(" ", microtime());
        $base_uid = date('ymd') + $uid;
        $order_no = 'gm' . $_sec . substr($_u_sec, 2, 3) . sprintf("%08d", $base_uid) . Str::random(8);
        $data     = [
            'order_no'      => $order_no,
            'uid'           => $uid,
            'money'         => 0,
            'day'           => $vip_detail['day'],
            'vip_type'      => $vip_detail['type'],
            'vip_config_id' => $vip_detail['id'],
            'pay_type'      => 99,
            'status'        => 1,
            'update_time'   => time(),
            'create_time'   => time(),
            'date_v'        => myDate(),
        ];
        self::create($data);
        return true;
    }
}